//
// Created on 2024/7/1.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef DOUBLE_QUEUE_SURFACE_MODE_VIDEO_ENCODER_OHOS_GLDRAWER_H
#define DOUBLE_QUEUE_SURFACE_MODE_VIDEO_ENCODER_OHOS_GLDRAWER_H

#include <map>
#include <native_window/external_window.h>
#include "egl_render_context.h"
#include "shader_program.h"

namespace webrtc {
namespace ohos {

enum class TextureType { OES, RGB, YUV };

struct TextureBuffer {
    GLuint textureID;
    TextureType frameType;
};

class OhosGLDrawer {
public:
    OhosGLDrawer() = default;
    ~OhosGLDrawer();
    bool PrePareShader(TextureType frameType);      //shaderProgram初始化
    void ShaderRelease();                           //shaderProgram释放
    void CreateGLResources();                       //创建GL画布
    void CleanGLResources();                        //清理GL画布
    void DrawFrame(TextureBuffer buffer, float *matrix, int frameWidth, int frameHeight, int viewportX, int viewportY,
      int viewportWidth, int viewportHeight);       //绘制图像到编码器显存
private:
    void DrawOES(GLuint textureID, float *matrix, int frameWidth, int frameHeight, int viewportX, int viewportY,
      int viewportWidth, int viewportHeight);
    void DrawRgb(int textureId, float *matrix, int frameWidth, int frameHeight,
      int viewportX, int viewportY, int viewportWidth, int viewportHeight);
    void DrawYuv(int *yuvTextures, float *matrix, int frameWidth, int frameHeight,
      int viewportX, int viewportY, int viewportWidth, int viewportHeight);
    TextureType currentFrameType_ {TextureType::OES};
    std::unique_ptr<ShaderProgram> shaderProgram_;
    static std::map<TextureType, std::string> shaderMap_;
    GLuint vertexArrayObject_ = 0;
    GLuint vertexBufferObject_ = 0;
    float matrix_[16]{1.0, 0.0, 0.0, 0.0, 
                      0.0, -1.0, 0.0, 0.0, 
                      0.0, 0.0, 1.0, 0.0, 
                      0.0, 0.0, 0.0, 1.0};
};

}
}

#endif //DOUBLE_QUEUE_SURFACE_MODE_VIDEO_ENCODER_OHOS_GLDRAWER_H
